package edu.nf.ch03.service;

import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.vectorstore.QuestionAnswerAdvisor;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.stereotype.Service;

/**
 * @author wangl
 * @date 2025/4/19
 */
@Service
@RequiredArgsConstructor
public class RagChatService {

    private final ChatModel chatModel;

    /**
     * 注入向量数据库
     */
    private final VectorStore vectorStore;


    public String chat(String prompt) {
        SearchRequest searchRequest = SearchRequest.builder().topK(5).build();
        ChatResponse response = ChatClient.builder(chatModel).build()
                .prompt()
                .advisors(new QuestionAnswerAdvisor(vectorStore, searchRequest))
                .system("你是一名问答小助手，请根据检索的内容思考后进行并回答，不要出现\"根据提供的信息\"等参考痕迹。如果不清楚，你只需要回答：\n" +
                        "很抱歉，我没有您要咨询的信息，但您可以联系相关的老师或者到学校官网了解更多详情")
                .user(prompt)
                .call()
                .chatResponse();
        return response.getResult().getOutput().getText();
    }
}